package com.yssw.client.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.yssw.common.annotation.Anonymous;
import com.yssw.model.vo.article.ArticleVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yssw.common.annotation.Log;
import com.yssw.common.core.controller.BaseController;
import com.yssw.common.core.domain.AjaxResult;
import com.yssw.common.enums.BusinessType;
import com.yssw.client.domain.ArticleTable;
import com.yssw.client.service.IArticleTableService;
import com.yssw.common.utils.poi.ExcelUtil;
import com.yssw.common.core.page.TableDataInfo;

/**
 * 文章Controller
 *
 * @author yssw
 * @date 2023-04-01
 */
@Anonymous
@RestController
@RequestMapping("/client/article")
public class ArticleTableController extends BaseController {
    @Autowired
    private IArticleTableService articleTableService;

    /**
     * 查询文章列表
     */
//    @PreAuthorize("@ss.hasPermi('client:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArticleTable articleTable) {
        startPage();
        List<ArticleVo> list = articleTableService.selectArticleTableList(articleTable);
        return getDataTable(list);
    }

    /**
     * 导出文章列表
     */
//    @PreAuthorize("@ss.hasPermi('client:article:export')")
    @Log(title = "文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArticleTable articleTable) {
        List<ArticleVo> list = articleTableService.selectArticleTableList(articleTable);
        ExcelUtil<ArticleVo> util = new ExcelUtil<ArticleVo>(ArticleVo.class);
        util.exportExcel(response, list, "文章数据");
    }

    /**
     * 获取文章详细信息
     */
//    @PreAuthorize("@ss.hasPermi('client:article:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(articleTableService.selectArticleTableById(id));
    }

    /**
     * 新增文章
     */
    @PreAuthorize("@ss.hasPermi('client:article:add')")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArticleTable articleTable) {
        return toAjax(articleTableService.insertArticleTable(articleTable));
    }

    /**
     * 修改文章
     */
    @PreAuthorize("@ss.hasPermi('client:article:edit')")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArticleTable articleTable) {
        return toAjax(articleTableService.updateArticleTable(articleTable));
    }

    /**
     * 删除文章
     */
    @PreAuthorize("@ss.hasPermi('client:article:remove')")
    @Log(title = "文章", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(articleTableService.deleteArticleTableByIds(ids));
    }
}
